/**
 * 
 */
package eu.news.data.hibernate.dao;

import java.util.List;
import org.hibernate.Query;

import eu.news.data.dao.NewsDAO;
import eu.news.data.hibernate.pojo.HB_News;
import eu.news.data.pojo.News;

/**
 * @author WZZ
 *
 */
public class HB_NewsDAO extends HibernateBaseDAO<News> implements NewsDAO{
	
	protected HB_NewsDAO(){};
	
	public News getById(int id){
		String hql="from HB_News where id=:id";
		Query query=session.createQuery(hql);
		query.setInteger("id", id);
		return (HB_News) query.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<News> getAll(int start, int size) {
		String hql="from HB_News";
		Query query=session.createQuery(hql);
		query.setFirstResult(start);
		query.setMaxResults(size);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<News> getBySpecial(String specialName,int start, int size) {
		String hql="from HB_News where sort.special.name=:sp";
		Query query=session.createQuery(hql);
		query.setString("sp", specialName);
		query.setFirstResult(start);
		query.setMaxResults(size);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<News> getBySpecialSort(String specialName,String sortName,int start, int size) {
		String hql="from HB_News where sort.special.name=:sp and sort.name=:st";
		Query query=session.createQuery(hql);
		query.setString("sp", specialName);
		query.setString("st", sortName);
		query.setFirstResult(start);
		query.setMaxResults(size);
		return query.list();
	}

	@Override
	public News newEntity() {
		return new HB_News();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<News> getUnverified(int start, int size) {
		String hql="from HB_News where state=:state";
		Query query=session.createQuery(hql);
		query.setInteger(":state", News.STATE.STATE_UNVERIFIED);
		query.setFirstResult(start);
		query.setMaxResults(size);
		return query.list();
	}

}
